// JavaScript Document

function showAddProductDlg(obj)
{
	add_product_window.show(obj)
}

function showAddVendorDlg()
{
	add_vendor_window.show();
}
function deleteAssets()
{
	alert("delte  ----  "+grid_list.getSelectionModel().getSelections()[0].json);
}

function onFilterTypeSelected(id,name)
{
	alert("Filter Type: " + id + " -- " + name);
}

function onFilterProductSelected(id,name)
{
	alert("Filter Product Type: " + id + " -- " + name);
}
///////////////////////////////


var add_assets_Form;


var add_assets_post_url = "test.jsp";

///////Generate Assets List here
var myData = [
	["AMD 4200+","True","false","CPU","1","dexter","web"],
	["Gigabit 8800GT 512M DDR3","True","false","CPU","1","dexter","web"]
];

////////Generate Product List here
var productData = 
[
	["1","CPU"],
	["2","DVD-ROM"],
	["3","Keyboard"],
	["4","Video Card"]
]

///////Generate Vendor List Here
var vendorData = 
[
	["1","Intel"],
	["2","AMD"],
	["3","Dell"],
	["4","SONY"]
]

//////Generate Filter Type Here
var FilterType = 
[
 	["0","All"],
	["1","Unassigned"],
	["2","Not in contract"],
	["3","Leased Resources"],
	["4","Resources In Store"],
	["5","Resources In Use"],
	["6","Resources In Repair"],
	["7","Resources Expired"],
	["8","Resources Disposed "]
]

var FilterProduct = 
[
	["0","CPU"],
	["1","Main board"]
]

var ProductType = 
[
	["0","Access Point"],
	["1","Keybooard"],
	["2","Printer"],
	["3","Projector"],
	["4","Router"],
	["5","Scanner"],
	["6","Switch"]
]
/////////////////////////////////////////////
function createNewAssets()
{
	add_new_window.show(this.getEl());
}
/////////////////
//product name store
var store_product_name = new Ext.data.SimpleStore
({
	fields:["returnValue","textValue"]
})
store_product_name.loadData(productData);
//////////////////////////
// vendor list store
var store_vendor = new Ext.data.SimpleStore
({
	fields:["returnValue","textValue"]
})
store_vendor.loadData(vendorData);
//////////////////////////
// Filter Type Store
var filter_type_store = new Ext.data.SimpleStore
({
	fields:["returnValue","textValue"]
})
filter_type_store.loadData(FilterType);
///////////////////////////
// Filter Product Store
var filter_Product_store = new Ext.data.SimpleStore
({
	fields:["returnValue","textValue"]
})
filter_Product_store.loadData(FilterProduct);

//////////////////////////////
// Product Type Store
var product_type_store = new Ext.data.SimpleStore
({
	fields:["returnValue","textValue"]
})
product_type_store.loadData(ProductType);

//////////////////////////////
//Assets List Store
var store = new Ext.data.SimpleStore({
	fields: [
	   {name: 'Asset_Name'},
	   {name: 'Is_Leased'},
	   {name: 'Lease_Expiry'},
	   {name: 'Product'},
	   {name: 'Asset_State'},
	   {name: 'Associated_To'},
	   {name: 'User_Department'}
	]
});
store.loadData(myData);

function toggleDetails(btn, pressed){

}



///////////Filter Combobox
var filter_type_combo = new Ext.form.ComboBox
({
	xtype:'combo',
	store:filter_type_store,
	valueField:'returnValue',
	displayField:'textValue',
	mode:'local',
	forceSelection:true,
	blankText:'Please select a type.',
	editable:false,
	triggerAction:'all',
	
	name: 'form_filter_type',
	width:140,
	anchor:'100%'
})
filter_type_combo.on("select",function(a,b,c)
{
	onFilterTypeSelected(c,FilterType[c][1]);
})
/////////////Filter Product
var filter_product_combo = new Ext.form.ComboBox
({
	xtype:'combo',
	store:filter_Product_store,
	valueField:'returnValue',
	displayField:'textValue',
	mode:'local',
	forceSelection:true,
	blankText:'Please select a product.',
	editable:false,
	triggerAction:'all',
	
	name: 'form_filter_product',
	width:140,
	anchor:'100%'
})
filter_product_combo.on("select",function(a,b,c)
{
	onFilterProductSelected(c,FilterProduct[c][1]);
})






//////////////////////////////////////////
/////////////////Assets List Grid
//////////////////////////////////////////
	var grid_list = new Ext.grid.GridPanel({
        store: store,
        columns: [
            {id:'Assets_tb_id',header: "Asset Name", width: 180, sortable: true, dataIndex: 'Asset_Name'},
            {header: "Is Leased", width: 85, sortable: true,  dataIndex: 'Is_Leased'},
            {header: "Lease Expiry", width: 85, sortable: true,  dataIndex: 'Lease_Expiry'},
            {header: "Product", width: 85, sortable: true,  dataIndex: 'Product'},
			{header: "Asset State", width: 85, sortable: true,  dataIndex: 'Asset_State'},
			{header: "Associated To", width: 95, sortable: true,  dataIndex: 'Associated_To'},
            {header: "User Department", width: 105, sortable: true,  dataIndex: 'User_Department'}
        ],
		tbar: new Ext.Toolbar({
			items:[
				{
					text:"New",
					handler:createNewAssets
				},
				{
					text:"Delete",
					handler:deleteAssets
				},
				{
					xtype:"tbseparator"
				},
				{
					xtype:"tbspacer"
				},
				{
					xtype: 'tbtext',
					text: '<span style="font-weight:bold;">Filter :</span> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Viewing'
				},
				{
					xtype:"tbspacer"
				},
				filter_type_combo,
				{
					xtype:"tbspacer"
				},
				{
					xtype:"tbspacer"
				},
				filter_product_combo
			]
		}),
		bbar: new Ext.PagingToolbar({
            pageSize: 25,
            store: store,
            displayInfo: true,
            displayMsg: 'Displaying topics {0} - {1} of {2}',
            emptyMsg: "No topics to display",
            items:[
                '-', {
                pressed: true,
                enableToggle:true,
                text: 'Save Changes',
                cls: 'x-btn-text-icon details',
                toggleHandler: toggleDetails
            }]
        }),
        stripeRows: true,
        autoExpandColumn: 'Assets_tb_id',
        height:350,
        width:Ext.get("assets_list_content").getWidth(),
		autoWidth : true, 
        title:'Assets List'
    });

	
	try
	{
		viewport.on("resize",function(){
			grid_list.setWidth(Ext.get("assets_list_content").getWidth());
		})
	}catch(e){}
    grid_list.render('assets_list_content');
    grid_list.getSelectionModel().selectFirstRow();
//////////////////////////////////////////
/////////////////End Assets List Grid
//////////////////////////////////////////


/////////////////////////////////////////
////////////////Add new Assets Window
/////////////////////////////////////////


add_assets_Form = new Ext.FormPanel({
	width:600,
	bodyBorder :false,
	border :false,
	url:add_assets_post_url,
	labelAlign: 'top',
	onSubmit: Ext.emptyFn,
	submit: function() 
	{
		this.getForm().getEl().dom.action = this.url;
		this.getForm().getEl().dom.submit();
	},
	bodyStyle : "padding:10px;",
	items:[{
            layout:'column',
			border :false,
            items:[{
                columnWidth:.375,
                layout: 'form',
				border :false,
                items: [{
                    xtype:'combo',
					store:store_product_name,
					valueField:'returnValue',
					displayField:'textValue',
					mode:'local',
					forceSelection:true,
					blankText:'Please select a product.',
					editable:false,
					triggerAction:'all',
					allowBlank:false,
                    fieldLabel: 'Product Name',
                    name: 'add_product_form_name',
                    anchor:'100%'
                }, {
                    xtype:'textfield',
					allowBlank:false,
                    fieldLabel: 'Asset Name',
                    name: 'add_product_form_asset',
                    anchor:'100%'
                }, {
                    xtype:'textfield',
                    fieldLabel: 'Asset Tag',
                    name: 'add_product_form_tag',
                    anchor:'100%'
                }, {
                    xtype:'textfield',
                    fieldLabel: 'Asset Serial No',
                    name: 'add_product_form_serial',
                    anchor:'100%'
                }, {
                    xtype:'textfield',
                    fieldLabel: 'Bar Code',
                    name: 'add_product_form_bar',
                    anchor:'100%'
                }
				]
            },
			{
				columnWidth:.15,
				layout: 'form',
				border :false,
                items: [{
					border :false,
					html: '<a href="#" onclick="showAddProductDlg(this)">Add new</a>',
					style:"margin-top:19px",
					anchor:'80%'
                }]
			},
			{
                columnWidth:.375,
				border :false,
                layout: 'form',
                items: [{
                    xtype:'combo',
					store:store_vendor,
					valueField:'returnValue',
					displayField:'textValue',
					mode:'local',
					forceSelection:true,
					blankText:'Please select a vendor.',
					editable:false,
					triggerAction:'all',
					allowBlank:false,
                    fieldLabel: 'Vendor Name',
                    name: 'form_vendor_name',
                    anchor:'100%'
                }, {
                    xtype:'textfield',
					allowBlank:false,
                    fieldLabel: 'Asset Cost($)',
                    name: 'add_product_form_cost',
                    anchor:'100%'
                }, new Ext.form.DateField({

					fieldLabel: 'Acquisition Date',
					name: 'add_product_form_a_date',
					//format:'Y-m-d H:i:s',
					anchor:'100%'
                }), new Ext.form.DateField({

					fieldLabel: 'Expiry Date',
					name: 'add_product_form_e_date',
					//format:'Y-m-d H:i:s',
					anchor:'100%'
                }), 
				 {
                    xtype:'textfield',
                    fieldLabel: 'Location',
                    name: 'add_product_form_location',
                    anchor:'100%'
                }]
            },
			{
				columnWidth:.1,
				layout: 'form',
				border :false,
                items: [{
					border :false,
					html: '<a href="#" onclick="showAddVendorDlg()">Add new</a>',
					style:"margin-top:19px",
					anchor:'100%'
                }]
			}]
        }]
});
function sendAddRequest()
{
	add_assets_Form.submit();
}
var add_new_window = new Ext.Window({
	title: 'Add New Assets',
	width: 630,
	height:350,
	minWidth: 300,
	minHeight: 200,
	layout: 'fit',
	plain:true,
	bodyStyle:'padding:5px;',
	buttonAlign:'center',
	items: add_assets_Form,
	modal:true,
	buttons: [{
		text: 'Add',
		handler:function(){sendAddRequest();}
	},{
		text: 'Cancel',
		handler:function(){add_new_window.hide();}
	}]
})

add_new_window.on("beforeclose",function(){add_new_window.hide();return false;})

//add_assets_Form.render(document.body);
/////////////////////////////////////////
////////////////End Add new Assets Window
/////////////////////////////////////////



////////////////
//Add product form
var add_product_form = new Ext.FormPanel
({
	
	bodyBorder :false,
	border :false,
	url:add_assets_post_url,
	bodyStyle : "padding:10px;",
	onSubmit: Ext.emptyFn,
	items:
	[{
			border :false,
			html: '<span style="font-weight:bold;">Enter the product details here</span>',
			style:"margin-bottom:6px",
			anchor:'80%'
     },{
		xtype:'textfield',
		allowBlank :false,
		fieldLabel: '<span style="color:red;">*</span>Product Name',
		name: 'add_prd_form_Prd_n',
		anchor:'100%'
	},
	{
                    xtype:'combo',
					store:product_type_store,
					valueField:'returnValue',
					displayField:'textValue',
					mode:'local',
					forceSelection:true,
					blankText:'Product Type',
					editable:false,
					triggerAction:'all',
					allowBlank:false,
                    fieldLabel: '<span style="color:red;">*</span>Product Type',
                    name: 'add_prd_form_Prd_t',
                    anchor:'100%'
	},
	{
		xtype:'textfield',
		fieldLabel: 'Price($)',
		name: 'add_prd_form_Prd_p',
		anchor:'100%'
	}
	]
})

var add_product_window = new Ext.Window({
	title: 'Add New Product',
	width: 430,
	height:200,
	minWidth: 300,
	minHeight: 200,
	layout: 'fit',
	plain:true,
	buttonAlign:'center',

	items:add_product_form,
	modal:true,
	buttons: [{
		text: 'Add',
		handler:function(){sendAddProductRequest();}
	},{
		text: 'Cancel',
		handler:function(){add_product_window.hide();}
	}]
})
add_product_window.on("beforeclose",function(){add_product_window.hide();return false;})
function sendAddProductRequest()
{
	add_product_form.submit();
}
////////////////
//Add vendor form
var add_vendor_form = new Ext.FormPanel
({
	
	bodyBorder :false,
	border :false,
	url:add_assets_post_url,
	bodyStyle : "padding:10px;",
	onSubmit: Ext.emptyFn,
	items:
	[{
			border :false,
			html: '<span style="font-weight:bold;">Please enter the details of the vendor</span>',
			style:"margin-bottom:6px",
			anchor:'80%'
     },{
		xtype:'textfield',
		allowBlank :false,
		fieldLabel: '<span style="color:red;">*</span>Name',
		name: 'add_vd_form_n',
		anchor:'100%'
	},
	new Ext.form.TextArea
	({    
		fieldLabel: 'Description',
		name: 'add_vd_form_des',
		width:200
    }),
	{
		xtype:'textfield',
		fieldLabel: 'Contact Person',
		name: 'add_vd_form_Prd_p',
		anchor:'100%'
	}
	]
})

var add_vendor_window = new Ext.Window({
	title: 'Add New Vendor',
	width: 430,
	height:250,
	minWidth: 300,
	minHeight: 200,
	layout: 'fit',
	plain:true,
	buttonAlign:'center',

	items:add_vendor_form,
	modal:true,
	buttons: [{
		text: 'Add',
		handler:function(){sendAddVendorRequest();}
	},{
		text: 'Cancel',
		handler:function(){add_vendor_window.hide();}
	}]
})
add_vendor_window.on("beforeclose",function(){add_vendor_window.hide();return false;})
function sendAddVendorRequest()
{
	
}

